﻿using System;
using System.Collections.Generic;
using System.Text;
using KaiXin001er.WebBrowser;
using KaiXin001er.UTIL;
using KaiXin001er.Objects;

namespace KaiXin001er.Logic
{
    public class FriendPlayer:BasePlayer
    {
        public List<Friend> Friends
        {
            get { return Account.Friends; }
        }
        private int friendsPerpage = 40;

        public override bool SignOn()
        {
            return true;
        }
        public FriendPlayer(Account acct) : base(acct) { }
        public override bool SignOff()
        {
            return true;
        }

        public override bool PlayPlay()
        {
            //这里面循环所有页面读取好友信息 主要是姓名和客户号
            Friends.Clear();
            WebAccessor dac = WebAccessor.Instance;
            int start = 0;
            RaiseMsg("[好友模块] 读取好友");
            请求参数 pageInfo = new 请求参数();            
            while (true)
            {
                
                string ret = dac.Request(URLS.我的好友, pageInfo.ToString(), WebAccessor.RequestMethod.GetMethod);
                //循环读取
                ///如果存在就更新一下姓名
                List<Friend> fl =页面分析者.ReadFriendsFromOnPage(ret);
                foreach (Friend f in fl)
                {
                    if (HasFriend(f.FriendID))
                    {
                        FindFriendByID(f.FriendID).FriendName = f.FriendName;
                    }
                    else
                    {
                        Friends.Add(f);
                    }
                }
                //如果没有下一页链接就说明读取结束了
                if (页面判断者.好友最后一页吗(ret)) break;
                start += friendsPerpage;
                pageInfo = new 请求参数();
                pageInfo.add("uid", this.Account.PlayerID);
                pageInfo.add("viewtype", "");
                pageInfo.add("start",start.ToString());

            }
            RaiseMsg("[好友模块] 好友列表");
            for (int i = 1; i <= Friends.Count;i++ )
            {
                RaiseMsg(string.Format("[{0}]好友ID: {1} 好友姓名:{2} ",i, Friends[i-1].FriendID, Friends[i-1].FriendName));
            }
            return true;
        }
    }
}
